package com.qidian.study.dao.impl;

import com.qidian.study.dao.AdminDAO;
import com.qidian.study.pojo.Admin;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @author 戴着假发的程序员
 * @company 起点编程
 */
public class AdminDAOImpl extends BaseDAO implements AdminDAO {
    private Connection con;
    private PreparedStatement pst;
    private ResultSet rs;
    public Admin queryByName(String adminName) {
        Admin admin = null;
        String sql = "select * from v_admin where admin_name = ?";
        try{
            con = getCon();
            pst = con.prepareStatement(sql);
            pst.setString(1,adminName);
            rs = pst.executeQuery();
            if(rs.next()){
                admin = new Admin();
                admin.setAdminId(rs.getLong("admin_id"));
                admin.setAdminName(rs.getString("admin_name"));
                admin.setAdminPass(rs.getString("admin_pass"));
                admin.setAdminRole(rs.getInt("admin_role"));
                admin.setCreateTime(rs.getString("create_time"));
                admin.setModifyTime(rs.getString("modify_time"));
                admin.setStatus(rs.getInt("status"));
            };
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(rs,pst,con);
        }
        return admin;
    }

    public int save(Admin admin) {
        String sql = "insert into v_admin(admin_name,admin_pass,admin_role,create_time,modify_time,status) values(?,?,?,?,?,?)";
        // 调用父类执行修改操作的方法
        return executeUpdate(sql,admin.getAdminName(),admin.getAdminPass(),admin.getAdminRole(),admin.getCreateTime(),admin.getModifyTime(),admin.getStatus());
    }
}
